﻿using System;
using System.Threading;
using System.Timers;
using OptimalisationFramework.Interfaces;

namespace OptimalisationFramework.Utilities
{
    class ThreadedSearch : IThreadSearch
    {
        // thread
        private Thread tr;
        private IMetaHeuristic mh;

        // events

        public ThreadedSearch(IMetaHeuristic mh)
        {
            this.mh = mh;
            tr = new Thread(mh.Start);
        }

        public ThreadedSearch(IMetaHeuristic mh, string threadName)
        {
            this.mh = mh;
            tr = new Thread(mh.Start);          
            tr.Name = threadName;
        }

        public string Name()
        {
            return tr.Name;
        }

        public void Start()
        {
            tr.Start();
        }

        public void AddDelegate(TimerCallback timerDelegate, int numberOfRepeats)
        {
            /*timerDelegate = new TimerCallback(printTime);
            AutoResetEvent autoEvent     = new AutoResetEvent(false);
            threadTimer = new System.Threading.Timer(timerDelegate, autoEvent, 1000, 250);
            autoEvent.WaitOne(1000, false);
            threadTimer.Dispose();
            */

        }

        public void printTime(object stateinfo)
        {
            Console.WriteLine(DateTime.Now);
        }
        public bool IsAlive()
        {
            return tr.IsAlive;
        }

        public void Stop()
        {
            tr.Abort();                     
        }

        public string Duration()
        {
            return mh.Duration();
        }
    }
}
